package com.bjesc_b2c.app.appraiset.mvp.model.impl;

import android.content.Context;

import com.bjesc_b2c.app.appraiset.http.IApiFragmentRequest;
import com.bjesc_b2c.app.appraiset.mvp.model.CarSourcePhotoModel;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import master_bean.CarSourceImageBean;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/3/3.
 */

public class CarSourcePhotoModelImpl implements CarSourcePhotoModel {
    private CallBack callBack;

    public CarSourcePhotoModelImpl(CallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void getPhoto(Context context, final String sourceID) {
        HashMap<String, Object> data = new MapUtil()
                .put("car_id", CustomSharedPreferences.getString(context, KeyBean.CARID.valueOf(), KeyBean.FILE_NAME.valueOf()))
//                .put("class_id", sourceID)
                .put("type", sourceID)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, data).create(IApiFragmentRequest.class).getSourceImg()
                , new BaseSubscriber<TokenBean<CarSourceImageBean>>(context) {
                    @Override
                    public void onSuccess(TokenBean<CarSourceImageBean> o) {
                        if (callBack != null) {
                            if (o.getData() != null) {
                                if (o.getData().getImg_info() != null && o.getData().getImg_info().size() > 0) {
                                    callBack.callBack(o.getData(), sourceID);
                                } else {
                                    callBack.callBack(null, sourceID);
                                }
                            } else {
                                callBack.callBack(null, sourceID);
                            }
                        }
                    }
                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    @Override
    public void savePhoto(Context context, String classId, String json) {
        HashMap<String, Object> data = new MapUtil()
                .put("id", CustomSharedPreferences.getString(context, KeyBean.CARID.valueOf(), KeyBean.FILE_NAME.valueOf()))
//                .put("class_id", classId)
                .put("img_info", json)
                .build();

        RxRequest.request(ShowType.SHOW, RxRequest.getClient(context, data).create(IApiFragmentRequest.class).saveSourceImg()
                , new BaseSubscriber<TokenBean>(context) {
                    @Override
                    public void onSuccess(TokenBean o) {

                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    public interface CallBack {
        void callBack(CarSourceImageBean imageBean, String sourceID);
    }
}
